package main

import (
	"fmt"
	"strconv"
	"sync"
	"time"
)

func main() {

	c := make(chan int)
	for i := 0; i < 20; i++ {
		name := "ID:" + strconv.Itoa(i)
		go func() {
			for {
				time.Sleep(1 * time.Second)
				d9_deposit(100)
				fmt.Println("name:", name)
			}
		}()
	}

	// 读取数据，这里是永久阻塞，防止main退出
	fmt.Println(<-c)
}

// 账号
var (
	// 使用互斥锁
	d9Lock sync.Mutex // guards balance
	//一个只能为1和0的信号量叫做二元信号量(binary semaphore)
	d9Balance int
)

// 存钱
func d9_deposit(amount int) {
	d9Lock.Lock()
	defer d9Lock.Unlock()
	d9Balance += amount
	time.Sleep(1 * time.Second)
}
